package com.university.mentalhealthplatform.global.rbac.service;

import com.university.mentalhealthplatform.global.rbac.model.CustomUserDetails;
import com.university.mentalhealthplatform.global.rbac.model.User;
import com.university.mentalhealthplatform.global.rbac.repository.BaseUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final BaseUserRepository baseUserRepository;

    @Autowired
    public UserDetailsServiceImpl(BaseUserRepository baseUserRepository) {
        this.baseUserRepository = baseUserRepository;
    }

    @Override
    public CustomUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = baseUserRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));


        // 使用 CustomUserDetails 封装用户信息，包括角色和权限
        return new CustomUserDetails(user);
    }
}
